<!DOCTYPE html>
<meta charset="UTF-8">
<form id="myselectdata" style="margin-top: 20px">
	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">名称：</label>
			<div class="layui-input-inline">
				<input type="text" name="name" id="name" class="layui-input" placeholder="请输入权限名称"/>
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">代码：</label>
			<div class="layui-input-inline">
				<input type="text" name="code" id="code" class="layui-input" placeholder="请输入代码"/>
			</div>
		</div>

		<div class="layui-inline">
			<div class="layui-btn-group ">
				<button type="button" class="layui-btn layui-icon layui-icon-search"onclick="search()">查询</button>
				<button type="button" class="layui-btn layui-icon layui-icon-circle"onclick="empty()">清空</button>
			</div>
		</div>
	</div>
</form>
<script type="text/html" id="toolbar">
	<div class="layui-btn-group ">
   		<button class="layui-btn layui-icon layui-icon-add-1" lay-event="add"> 新增</button>
  	  	<button class="layui-btn layui-icon layui-icon-edit" lay-event="update"> 修改</button>
  	  	<button class="layui-btn layui-icon layui-icon-delete" lay-event="delete"> 删除</button>
	</div>
</script>
<table id="permission_table" lay-filter="test"></table>

<script>
layui.use('table', function(){
  var table = layui.table;
  table.render({
	  	title:'学生列表',
	  	color:'#666',
		height: 'auto',
	    elem: '#permission_table',
	    url:'permission/page',
	    toolbar: '#toolbar',
	    page: true,
	    cols: [
	    	[
	      {type:'checkbox'},
	      {field:'name', width:'48%', title: '权限名称'},
	      {field:'code', width:'48%', title: '代码'}
	    ]
	    	]
  });
  table.on('toolbar',function(obj){
  	switch(obj.event){
      	case 'add':
      		add();
      		break;
      	case 'update':
      		update();
      		break;
      	case 'delete':
      		del();
      		break;
  	}
  });
  //新增
  function add(){
		layer.open({
	   	type:2,
	   	title:'<i class="layui-icon">新增</i>',
	   	area: ['500px','500px'],
	   	content:['permission/edit'],
	   	btn:['提交','退出'],
	   	yes : function(index, layero) {
			var obj = layero.find("iframe")[0].contentWindow;
			var win = top.window['layui-layer-iframe' + index];
			var id = win.$("#id").val();
			var name = win.$("#name").val();
			var code = win.$("#code").val();
			$.post('permission/save', {id:id,name:name,code:code}, function(msg) {
				layer.msg(msg);
				red();
			});
			layer.closeAll('iframe');
		
		}
	  })
  }
  
  function update(){
	  var checkStatus = table.checkStatus('permission_table');
	  var data = checkStatus.data;
	  var zid = data.length==1?data[0].id: '';
	  if(data.length!=1){
		  layer.msg("请选择一行进行修改！");
		  return;
	  }
	  layer.open({
	    	type:2,
	    	title:'<i class="layui-icon">修改</i>',
	    	area: ['500px','500px'],
	    	content:['permission/edit?id='+zid],
	    	btn:['提交','退出'],
	    	yes : function(index, layero) {
				var obj = layero.find("iframe")[0].contentWindow;
				var win = top.window['layui-layer-iframe' + index];
				var id = win.$("#idx").val();
				var name = win.$("#name").val();
				var code = win.$("#code").val();
				$.post('permission/save', {id:id,name:name,code:code}, function(msg) {
					layer.msg(msg);
					red();
				});
				layer.closeAll('iframe');
			}
		  })
  }
  
  function del(){
	  var checkStatus = table.checkStatus('permission_table');
	  var data = checkStatus.data;
	  var zid = data.length==1?data[0].id: '';
	  if(data.length!=1){
		  layer.msg("请选择一行进行删除！");
		  return;
	  }
	  layer.confirm('确定删除此行数据吗？', function() {
		  $.post('permission/delete',{id:zid},function(result){
			  layer.msg(result);
			  red();
		  })
	  });
  }
  
});
	//刷新表格
	function red(){
		var table = layui.table;
	  	table.reload('permission_table', {
		  url: 'permission/page'
		});
	}
	//查询
	function search(){
		var name = $('#name').val();
		var code = $('#code').val();
		layui.use('table', function(){
			var table = layui.table;
			table.reload('permission_table',{
				url:'permission/page',
				where: {
					name: name,
					code: code
				}
			});
		});
	}
	//清空表格
	function empty(){
		
		$("#myselectdata")[0].reset();
		var name = $('#name').val();
		var code = $('#code').val();
		layui.use('table', function(){
			var table = layui.table;
		  	table.reload('permission_table', {
			  url: 'permission/page',
			  where: {
					name: name,
					code: code
				}
			});
		});
	}
</script>
